package tgc.tdu.tgq.demo.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.tdu.tgq.demo.entity.Course;
import tgc.tdu.tgq.demo.entity.Grade;
import tgc.tdu.tgq.demo.entity.Student;
import tgc.tdu.tgq.demo.entity.SysUser;
import tgc.tdu.tgq.demo.entity.TbClass;
import tgc.tdu.tgq.demo.security.UserUtils;
import tgc.tdu.tgq.demo.service.GradeService;
import tgc.tdu.tgq.demo.service.StudentService;
import tgc.tdu.tgq.demo.service.TbClassService;
import tgc.tdu.tgq.demo.web.form.GradeForm;

@Controller
@RequestMapping(value = "/grade")
public class GradeController {
	@Autowired
	private GradeService gradeService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private StudentService studentService;
	@Autowired
	private TbClassService tbClassService;

	@RequestMapping(value = "/classcourse")
	public void classcourse(ModelMap modelMap) {
	}
	@RequestMapping(value = "/classgrade")
	public void classgrade(ModelMap modelMap) {
	}
	@RequestMapping(value = "/selfgrade")
	public void selfgrade(ModelMap modelMap) {
	}
	// ----------个人成绩查询------------------
	@RequestMapping(value = "/personGrade")
	public String personGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		List<Grade> list = gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/personGrade";
	}

	// -------------通过id查找学生---------------
	@RequestMapping(value = "/classGrade")
	public String classGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		Student student = studentService.findById(id);
		modelMap.put("stu", student);
		return "grade/classGrade";
	}

	// -----------班级成绩查询-------------------
	@RequestMapping(value = "/allStudentGrade")
	public String list(ModelMap modelMap) {
		List<TbClass> tbClass = tbClassService.findAll();
		modelMap.put("list", tbClass);
		return "grade/allStudentGrade";
	}
	//------------班级成绩录入--------------
	@RequestMapping(value = { "/classcourse" }, method = RequestMethod.POST)
	@ResponseBody
	public Object classcourse(ModelMap modelmap, Integer bjId, Integer couId) {
		List<Grade> grade = gradeService.findByTbClassIdAndCourseId(bjId, couId);
		modelmap.put("grade", grade);
		return grade;
	}
	//------------个人成绩录入---------------
	@RequestMapping(value = { "/selfgrade" }, method = RequestMethod.POST)
	@ResponseBody
	public Object selfgrade(Integer bjId,Integer stuId,ModelMap modelMap) {
		List<Grade> grade=gradeService.findByTbClassIdAndStudnetId(bjId,stuId);
		modelMap.put("grade", grade);
		return grade;
	}
	
	//------------班级成绩查询-----------
	@RequestMapping(value = "bjChange")
	public String bjChange(Integer bjId,ModelMap modelMap) {
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> list=gradeService.findAllGradeByTbClassId(courses,bjId);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", list);
		return "grade/cjb";
		
		
	}
	//--------------保存-------------
	@RequestMapping(value = {"/save"}, method = RequestMethod.POST)
	@ResponseBody
	public Object save(GradeForm form) {
		Integer g=form.getGrade();
		Grade grade=gradeService.findById(form.getId());
		grade.setGrade(g);
		gradeService.save(grade);
		return true;	
	}
	//----------个人成绩查询-------------
	@RequestMapping(value = "/self")
	public String self(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		List<Grade> list=gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/self";
	}
	//---------班级成绩查询----------------
	@RequestMapping(value = "/classcj")
	public String classcj(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		Student student=studentService.findById(id);
		Integer bjId=student.getTbClass().getId();
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		modelMap.put("stu", student);
		return "grade/classcj";
	}
}
